/**
 * Copyright (C) 2013 Inera AB (http://www.inera.se)
 *
 * This file is part of Inera Certificate Proxy (http://code.google.com/p/inera-certificate-proxy).
 *
 * Inera Certificate Proxy is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Inera Certificate Proxy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package se.inera.certificate.proxy.mappings.local;

import com.google.common.base.Throwables;
import org.apache.commons.lang.StringUtils;
import se.inera.certificate.proxy.filter.RequestContext;
import se.inera.certificate.proxy.mappings.AbstractDispatcher;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

import static org.apache.commons.lang.StringUtils.substringAfter;
import static se.inera.certificate.proxy.utils.PathUtil.getFirstSegment;
import static se.inera.certificate.proxy.utils.PathUtil.getPath;
import static se.inera.certificate.proxy.utils.PathUtil.joinFragments;

public class LocalDispatcher extends AbstractDispatcher {
    private final LocalMapping localMapping;

    public LocalDispatcher(LocalMapping localMapping, RequestContext requestContext) {
        super(requestContext);
        this.localMapping = localMapping;
    }

    @Override
    public void dispatch(HttpServletRequest request, HttpServletResponse response) {
        // Request path without servlet context path.
        String requestPath = getPath(request);

        // The new path to forward to including the new servlet context path.
        String newUri = joinFragments(localMapping.getMappedPath(), substringAfter(requestPath, localMapping.getContext()));

        // The context path of the 'forwarded to' servlet.
        String servletContextPath = getFirstSegment(newUri);

        // Servlet context of the 'forwarded to' servlet.
        ServletContext context = request.getServletContext().getContext(servletContextPath);

        // Get a request dispatcher for the new URL.
        RequestDispatcher requestDispatcher = context.getRequestDispatcher(StringUtils.substringAfter(newUri, servletContextPath));

        LocalRequestWrapper w = new LocalRequestWrapper(request);

        for (Map.Entry<String, String> header: getRequestContext().getHeaders(request).entrySet()) {
            w.addHeader(header.getKey(), header.getValue());
        }

        try {
            requestDispatcher.forward(w, response);
        } catch (ServletException e) {
            Throwables.propagate(e);
        } catch (IOException e) {
            Throwables.propagate(e);
        }
    }
}
